package com.hc.dfs.handle;

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.hc.dfs.activity.R;
import com.hc.dfs.anim.CommonGesture;
import com.hc.dfs.entities.flow.UnsignedNode;

/**
 * @author 王洪胜 E-mail:
 * @version 创建时间：2013-3-5 上午10:27:42
 * 
 * 类说明：详情页抽象类，设定详情页的布局文件
 * 
 */
public abstract class DetailPage {
	public Activity activity;
	public Context context;
	public UnsignedNode data;
	public View view;

	private CommonGesture cg;
	private ScrollView sv;
	private RelativeLayout ll;
	public RelativeLayout btn1;
	public RelativeLayout btn2;
	public RelativeLayout btn3;
	public Button back_btn;


	public DetailPage(Context context, int page, int docType) {
		this.context = context;
		this.activity = (Activity) context;
		setView(docType);
		setBaseInfo();
		setData();
		setOperButton(page);
		initTouch();
		initScreen();
	}

	// 设置布局文件
	public void setView(int docType) {
		LayoutInflater inflate = LayoutInflater.from(context);
		switch (docType) {
		case 4:
			view = inflate.inflate(R.layout.detail_chengpidan, null);
			break;
		case 5:
			view = inflate.inflate(R.layout.detail_fawengao, null);
			break;
		case 6:
			view = inflate.inflate(R.layout.detail_hetong, null);
			break;

		default:
			break;
		}
		activity.setContentView(view);
	};

	// 设置基本信息
	public abstract void setBaseInfo();

	// 设置填充数据
	public abstract void setData();

	// 设置显示的操作按钮
	public void setOperButton(int page) {
		System.out.println("设置显示的操作按钮");
		back_btn = (Button) activity.findViewById(R.id.button1);
		btn1 = (RelativeLayout) activity.findViewById(R.id.btn1);
		btn2 = (RelativeLayout) activity.findViewById(R.id.btn2);
		btn3 = (RelativeLayout) activity.findViewById(R.id.btn3);
		TextView btn1_txt = (TextView) activity.findViewById(R.id.btn1_txt);
		TextView btn2_txt = (TextView) activity.findViewById(R.id.btn2_txt);
		TextView btn3_txt = (TextView) activity.findViewById(R.id.btn3_txt);
		switch (page) {
		case 0:
			btn1_txt.setText("签批公文");
			btn2_txt.setText("申请延时");
			btn3_txt.setText("查看路径");
			break;
		case 1:
			btn1.setVisibility(View.GONE);
			btn2_txt.setText("跟踪进度");
			btn3_txt.setText("查看路径");
			break;
		case 2:
			System.out.println("我是3啊 进来没？");
			btn1.setVisibility(View.GONE);
			btn2.setVisibility(View.GONE);
			btn3_txt.setText("删除公文");
			break;
		default:
			break;
		}
	}

	// 处理触屏事件
	 private void initTouch() {
		 System.out.println("触摸事件");
		 sv = (ScrollView) activity.findViewById(R.id.doc_scroller);
		 ll = (RelativeLayout) activity.findViewById(R.id.ll_touch);
		 // 实例化手势控制对象
		 cg = new CommonGesture(1, context, null, 0, 0, 1, R.anim.translate0_0,
		 R.anim.translate0_100_300);
		 sv.setOnTouchListener(new OnTouchListener() {
		
			 @Override
			 public boolean onTouch(View v, MotionEvent event) {
				 // 把滑动事件传递给ll
				 ll.dispatchTouchEvent(event);
				 return false;
			 }
		 });
		
		 ll.setOnTouchListener(new OnTouchListener() {
		
			 @Override
			 public boolean onTouch(View v, MotionEvent event) {
				 ll.onInterceptTouchEvent(event);
				 // 把触屏事件传递GestureDetector对象处理
				 return cg.getGd().onTouchEvent(event);
			 }
		 });
	 }

//	 处理屏幕分辨率
	 public void initScreen() {
		 DisplayMetrics metric = new DisplayMetrics();
		 activity.getWindowManager().getDefaultDisplay().getMetrics(metric);
		 // 获取屏幕高度px
		 int height = metric.heightPixels;
		 // 获取屏幕密度dpi
		 int densityDpi = metric.densityDpi;
		 // 把屏幕高度单位由px换算成dp
		 int h = height / (densityDpi / 160);
		 // 屏幕高度大于480dp重新设置ll高度
		 if (h > 480) {
			 ll.getLayoutParams().height = (h - 40) * (densityDpi / 160);
		 }
	 }

	// 监听返回按键
	public void onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			activity.finish();
			activity.overridePendingTransition(R.anim.translate0_0,
					R.anim.translate0_100_300);
		}
	}

}
